Solving Systems of Non-Linear Equations

In this Notebook we learn how to solve systems of non-linear equations.

We will solve the following system of equations: $$\begin{array}{lll} & x^3 + y = 1 \\ & y^3 − x = −1 \end{array}.$$

You can easily check that $(x, y) = (1, 0)$ is a solution of this system. By graphing both of the equations you can also see that $(1, 0)$ is the only solution.


In [1]:
import numpy as np

x1 = np.linspace(-4,4,100) # 100 linearly spaced numbers
y1 = -x1**3+1

y2 = np.linspace(-4,4,100) # 100 linearly spaced numbers
x2 = y2**3+1

In [3]:
import matplotlib.pyplot as plt
%matplotlib inline

# compose plot
plt.plot(x1,y1)
plt.plot(x2,y2)
plt.xlim(-4.0, 4.0)
plt.ylim(-4.0, 4.0)
plt.xlabel("x")
plt.ylabel("y")
plt.title('$x^3+y=1$\n$y^3-x=-1$')
plt.show() # show the plot


We can put these equations into vector-function form by letting $x_1 = x$, $x_2 = y$ and $f_1(x_1, x_2) = x_1^3 + x_2 − 1$, $f_2(x_1, x_2) = x_2^3 − x_1 + 1.$

Define function equations that computes $f_1$ and $f_2$.


In [4]:
from scipy.optimize import fsolve

def equations(p):
    x, y = p
    return (x**3+y-1, y**3-x+1)

Solve equations and print solution.


In [5]:
x, y = fsolve(equations, (0.5, 0.5))
x, y


Out[5]:
(1.0000000000081608, -1.652294360384222e-11)

Check solution.


In [6]:
equations((x, y))


Out[6]:
(7.9594109081426723e-12, -8.1608053648096757e-12)